<template>
  <div class="login">
    <div class="login-box">
      <div class="top">
        <div class="">二次验证</div>
      </div>
      <div class="mid" style="text-align:center;">
        <el-form :model="dataForm"
                 :rules="dataRule"
                 ref="dataForm"
                 @keyup.enter.native="dataFormSubmit()"
                 status-icon>
          <el-form-item prop="authCode">
            <el-input class="info"
                      v-model="dataForm.authCode"
                      placeholder="请输入谷歌身份验证器上的验证码"></el-input>
          </el-form-item>
		   <div v-if="registerFlag"  style="text-align:center;margin-bottom: 25px;" >
				<span style="text-align:center;" >第一次登录，请使用谷歌验证器先扫描此二维码，再输入验证码</span>
				<div style="text-align:center;" ref="qrCodeUrl"></div>
		  </div>
		 <div id="showinfo" v-if="showglogin">
				注意：第一次登录系统，系统校验完帐号和密码后，会弹出"二步验证"的二维码。身份验证器微信扫描(或谷歌验证器):
				<img src="~@/assets/img/zkycauth.png" style="width: 200px;height: 200px"/>
		<!--        <p>-->
		<!--        安卓：<a target="_blank" href="${request.contextPath}/statics/image/authenticator.apk">authenticator.apk</a>-->
		<!--        </p>-->
		<!--        <p>-->
		<!--        苹果用户可直接苹果应用商店(appstore)下载-->
		<!--        </p>-->
			</div>
          <el-form-item>
            <div class="item-btn"><input type="button"
                     value="登录"
                     @click="dataFormSubmit()"></div>
          </el-form-item>
        </el-form>
      </div>

      <div class="bottom">Copyright © 2023 abc科技有限公司</div>
    </div>
    
  </div>
</template>

<script>
import { getUUID } from '@/utils'

import { encrypt } from '@/utils/crypto'


import QRCode from 'qrcodejs2'

export default {
	
  components: {
  },
  data () {
    return {
	  showglogin: false,
	  captchaPath:'',
	  registerFlag: false,
	  qrcodeValue:'',
	  qrcodeSize:200,
      dataForm: {
        authCode: ''
      },
      dataRule: {
        authCode: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  beforeDestroy () {
    document.removeEventListener('keyup', this.handerKeyup)
  },
  created () {
    this.getCaptcha()

    document.addEventListener('keyup', this.handerKeyup)
  },
  methods: {
    handerKeyup (e) {
      var keycode = document.all ? event.keyCode : e.which
      if (keycode === 13) {
        this.dataFormSubmit()
      }
    },

    // 提交表单
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
		   this.login();
        }
      })
    },
    login () {
      if (this.isSubmit) {
        return
      }
      this.isSubmit = true
	  var tempToken = this.$cookie.get("tempToken");
      this.$http({
        url: this.$http.adornUrl('/glogin?authCode='+ this.dataForm.authCode+"&tempToken="+tempToken),
        method: 'post',
        data: {
          'authCode': this.dataForm.authCode,
          'tempToken': tempToken
        }
      }).then(({ data }) => {
		if(data.code && data.code==500){
			 this.$message({
			   message: data.msg,
			   type: 'error',
			   duration: 2000,
			   onClose: () => {
				   this.isSubmit = false
			   }
			 })
		}else{
			var ret = data.data;
			this.$cookie.set('Authorization', ret.accessToken)
			this.$router.replace({ name: 'home' })
		}
      }).catch(() => {
        this.isSubmit = false
      })
    },
   
    // 获取验证码
    getCaptcha () {
	  var tagNum = this.$cookie.get("tagNum");
	  if(tagNum<=0){
		  this.showglogin=true;
		  this.registerFlag = true;
		  var tempToken = this.$cookie.get("tempToken");
		  this.$http({
		    url: this.$http.adornUrl('/loginGenerateCode?tempToken='+tempToken),
		    method: 'post',
		    data: {'tempToken': tempToken}
		  }).then(({ data }) => {
			  this.qrcodeValue = data.authUrl;
			  var qrcode = new QRCode(this.$refs.qrCodeUrl, {
			              text: data.authUrl, // 需要转换为二维码的内容
			              width:200,
			              height: 200,
			              colorDark: '#000000',
			              colorLight: '#ffffff',
			              correctLevel: QRCode.CorrectLevel.H
			          })
				console.log(qrcode);
		  }).catch(() => {
		    this.isSubmit = false
		  })
	  }
    },
  }
}
</script>

<style lang="scss">
.login {
  width: 100%;
  height: 100%;
  background: url(~@/assets/img/login-bg.png) no-repeat;
  background-size: cover;
  position: fixed;
}
.login .login-box {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  height: 100%;
  padding-top: 10%;
}
.login .login-box .top {
  margin-bottom: 30px;
  text-align: center;
}
.login .login-box .top .logo {
  font-size: 0;
  max-width: 50%;
  margin: 0 auto;
}
.login .login-box .top .company {
  font-size: 16px;
  margin-top: 10px;
}
.login .login-box .mid {
  font-size: 14px;
  width: 400px;
}
.login .login-box .mid .item-btn {
  margin-top: 20px;
}
.login .login-box .mid .item-btn input {
  border: 0;
  width: 100%;
  height: 40px;
  box-shadow: 0;
  background: #1f87e8;
  color: #fff;
  border-radius: 3px;
}
.info {
  width: 410px;
}
.login-captcha {
  height: 40px;
}
.login .login-box .bottom {
  position: absolute;
  bottom: 10%;
  width: 100%;
  color: #999;
  font-size: 12px;
  text-align: center;
}
</style>
